package com.cgc.venus.server.job.service;

import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.utils.DateUtil;
import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.job.bean.AlarmInfo;
import com.cgc.venus.server.job.inter.AlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class AlarmServiceImpl extends AbstractBaseService implements AlarmService{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private final int PAGE_SIZE=5;

    @Override
    public AlarmInfo queryAlarm(int level, int status, String timeStart, String timeEnd, String queryKey, int currentPage) throws VenusException {
        AlarmInfo alarmInfo=new AlarmInfo(new ArrayList(),0);
        if(StringUtil.isEmpty(timeStart)){
            return alarmInfo;
        }
        if(StringUtil.isEmpty(timeEnd)){
            return alarmInfo;
        }
        String sql0="SELECT count(*) num FROM t_job_alarm WHERE Create_time BETWEEN ? AND ?";
        String sql1="SELECT date_format(Create_time,'%y-%m-%d %T') time, Alarm_msg alarmMsg, Alert_level alarmLevel, Alarm_type alarmType, `status` alarmStatus, Ns_name alarmNs FROM t_job_alarm WHERE Create_time BETWEEN ? AND ?";
        String sql2=" order by Create_time desc LIMIT ?, ?";
        List<Object> obj=new ArrayList<Object>();
        try {

            obj.add(DateUtil.dealDateFormat1(timeStart));
            obj.add(DateUtil.dealDateFormat1(timeEnd));
        } catch (ParseException e) {
            throw new VenusException(VenusResponseStatus.LOG_EXCEPTION, "时间格式有误!");
        }

        if(level!=-1){
            //-1 表示所有
            sql0+=" AND Alert_level = ?";
            sql1+=" AND Alert_level = ?";
            obj.add(level);
        }

        if(status!=-1){
            //-1 表示所有
            sql0+=" AND status = ?";
            sql1+=" AND status = ?";
            obj.add(status);
        }

        if(!StringUtil.isEmpty(queryKey)){
            sql0+=" AND Alarm_msg LIKE ?";
            sql1+=" AND Alarm_msg LIKE ?";
            obj.add("%" +queryKey+"%");
        }
        List<Map<String, Object>> r=jdbcTemplate.queryForList(sql0,obj.toArray());
        int count=Integer.parseInt(r.get(0).get("num").toString());
        int r0=count/PAGE_SIZE;
        int r1=count%PAGE_SIZE;
        if(r1!=0){
            r0+=1;
        }
        alarmInfo.setTotalPage(r0);
        alarmInfo.setTotal(count);
        obj.add((currentPage - 1) * PAGE_SIZE);
        obj.add(PAGE_SIZE);
        r=jdbcTemplate.queryForList(sql1+sql2,obj.toArray());
        alarmInfo.setAlarmData(r);
        return alarmInfo;
    }
}